package com.lifeng.zk.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.retry.RetryUntilElapsed;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

/**
 * Created by lifeng on 2018/6/15.
 */
public class CuratorTest {
    public static void main(String[] args) throws Exception {
        //刚开始重试间隔为1秒，之后重试间隔逐渐增加，最多重试不超过三次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        //最大重试次数，和两次重试间隔时间
        RetryPolicy retryPolicy1 = new RetryNTimes(3, 1000);
        //会一直重试直到达到规定时间，第一个参数整个重试不能超过时间，第二个参数重试间隔
        RetryPolicy retryPolicy2 = new RetryUntilElapsed(5000, 1000);
        String zkurl = "127.0.0.1:2181";
        //第一种方式 //最后一个参数重试策略
        final CuratorFramework client = CuratorFrameworkFactory.newClient(zkurl, 5000,5000,retryPolicy);

        //第二种方式
        /*CuratorFramework client1 = CuratorFrameworkFactory.builder().connectString(zkurl)
                .sessionTimeoutMs(5000)//会话超时时间
                .connectionTimeoutMs(5000)//连接超时时间
                .retryPolicy(retryPolicy)
                .build();*/

        client.start();
        //client1.start();

        //创建节点
        /*String node = client.create().creatingParentsIfNeeded() //若创建节点的父节点不存在会先创建父节点再创建子节点
                .withMode(CreateMode.PERSISTENT) //withMode节点类型，
                .forPath("/curator/aa/aaa");
        System.out.println("创建节点:"+node);*/

        /*String node1 = client.create().creatingParentsIfNeeded() //若创建节点的父节点不存在会先创建父节点再创建子节点
                .withMode(CreateMode.PERSISTENT) //withMode节点类型，
                .forPath("/curator/aa/aaa","1111".getBytes());
        System.out.println("创建节点:"+node1);*/

        /*String node2 = client.create().creatingParentContainersIfNeeded()
                .withMode(CreateMode.PERSISTENT) //withMode节点类型，
                .forPath("/curator/aa/aaa","1111".getBytes());
        System.out.println("创建节点:"+node2);*/

        //设置节点数据
        //client.setData().forPath("/curator","张说的".getBytes());
        //client.setData().forPath("/curator","张说的232".getBytes()).setVersion(-1);

        //获取数据
        /*byte[] dataBy = client.getData().forPath("/curator/aa/aaa");
        System.out.println(new String(dataBy));*/
        //获取数据获取状态信息
        /*Stat stat = new Stat();
        System.out.println(new String(client.getData().storingStatIn(stat).forPath("/curator/aa/aaa")));
        System.out.println(stat);*/

        //删除
        //client.delete().forPath("/aaa"); //只能删除叶子节点
        //client.delete().deletingChildrenIfNeeded().forPath("/aaa");//递归删除
        //client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/aaa");//guaranteed保障机制，若未删除成功，只要会话有效会在后台一直尝试删除

        //其他方法
        //检查节点是否存在
        /*Stat stat = client.checkExists().forPath("/test");
        System.out.println(stat == null ? "不存在":"存在");*/
        //获取子节点
        //System.out.println(client.getChildren().forPath("/"));
        //获取命名空间
        //System.out.println(client.getNamespace());

        //事物
        /*client.inTransaction()
                .create().withMode(CreateMode.PERSISTENT).forPath("/li","1111111".getBytes())
                .and()
                .setData().forPath("/li","22222222222".getBytes())
                .and()
                .delete().forPath("/li")
                .and()
                .commit();*/
        //异步处理
        System.out.println("异步创建开始");
        /*client.create().creatingParentsIfNeeded()
                .inBackground().forPath("/li","11111".getBytes());*/
        /*client.create().creatingParentsIfNeeded()
                .inBackground(new BackgroundCallback() {
                    public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                        //curatorFramework就是client对象
                        System.out.println(curatorFramework.getChildren().forPath("/"));
                        System.out.println("path:"+curatorEvent.getPath());
                        System.out.println("name:"+curatorEvent.getName());
                        System.out.println("data:"+curatorEvent.getData());
                        System.out.println("stat:"+curatorEvent.getStat());
                        System.out.println("type:"+curatorEvent.getType());
                    }
                }).forPath("/li","11111".getBytes());*/

        /*client.setData().inBackground(new BackgroundCallback() {
            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
                System.out.println(curatorFramework.getChildren().forPath("/"));
                System.out.println("path:"+curatorEvent.getPath());
                System.out.println("name:"+curatorEvent.getName());
                System.out.println("data:"+curatorEvent.getData());
                System.out.println("stat:"+curatorEvent.getStat());
                System.out.println("type:"+curatorEvent.getType());
            }
        },"11111111").forPath("/li");
        System.out.println("异步创建结束");
        Thread.sleep(100000);*/
        client.close();
    }
}
